/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apshibernate;

/**
 *
 * @author SUPORTE2
 */
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ClienteDAO {

    private Session session;

    public ClienteDAO(Session session) {
        this.session = session;
    }

    public void salva(Cliente p) {
        Transaction tx = this.session.beginTransaction();
        this.session.save(p);
        tx.commit();
    }

    public void remove(Cliente p) {
        Transaction tx = this.session.beginTransaction();
        this.session.delete(p);
        tx.commit();
    }

    public void atualiza(Cliente p) {
        Transaction tx = this.session.beginTransaction();
        this.session.update(p);
        tx.commit();
    }
    
    public Cliente procura(int id) {
        return (Cliente) 
                this.session.load(Cliente.class, id);
    }
    

    public List<Cliente> listaTudo() {
        return this.session.createCriteria(
                Cliente.class).list();
    }
    
    public List<Cliente> pagina(int inicio, int quantia) {
        return this.session.createCriteria(Cliente.class)
                .setMaxResults(quantia)
                .setFirstResult(inicio).list();
    }

    public List <Cliente> listacliente(int id){
        return session.createQuery(
                "from Cliente where id="+id).list();
    }

    public List <Cliente> listaclientesSQL(String sql){
        return session.createSQLQuery(sql).addEntity(
                Cliente.class).list();
    }
}